#include <QtGui/QApplication>
#include "dialog.h"
#include <QApplication>
#include "str.h"
#include <QEvent>
#include <QKeyEvent>
#include <QObject>
#include "bass.h"

double koef = 0.999;
double k = 2;
bool chorusIsTrue = false;
bool harmonizedIsTrue = false;
DWORD SoundSpeed = 88200;
const int KEYS = 30;
const WORD keys[KEYS] = {
        '1','2','3','4','5','6','7','8','9','0',
        'Q','W','E','R','T','Y','U','I','O','P',
        'A','S','D','F','G','Z','X','C','V','B'};

int place = 0;
int strings[150][88200];
int a[51] =  {
    82.41, 87.31, 92.50, 98.00, 103.80, 110.00, 116.54, 123.48, 130.82, 138.59,
    147.83, 155.56, 164.81, 174.62, 185.00, 196.00, 207.00, 220.00, 233.08, 246.96, 261.63, 277.18,
    293.66, 311.13, 329.63, 349.23, 369.99, 392.00, 415.30, 440.00, 466.16, 493.88, 523.25,
    554.36, 587.32, 622.26, 659.26, 698.46, 739.98, 784.00, 830.60, 880.00, 932.32, 987.75, 1046.50,
    1108.70, 1174.60, 1244.50, 1318.50, 1396.90, 1480.00};
std::deque<int> samples;
int position[6] = {E_first_number, B_minor_number, G_minor_number, D_minor_number, A_major_number, E_major_number};
HSTREAM streams[6];

void Error(const char *text)
{
        printf("Error(%d): %s\n",BASS_ErrorGetCode(),text);
        BASS_Free();
        ExitProcess(0);
}

void run(double freq)
{
        str cur_str(freq, 44100, koef);
        cur_str.pluck();
        int sample = 0;
        for (int i = 0; i < 30000; ++i)
        {
                sample = int(cur_str.sample() * 34767);
                samples.push_back(sample);
        }
}

void run_all()
{
    for (int i = 0; i < 51; ++i){
        run(a[i]);
        for (int j = 0; !samples.empty() && j < 88200; ++j){
                strings[i][j] = samples[0];
                if (chorusIsTrue && j > 88200 / 64)
                    strings[i][j] += samples[0] * 0.99;
                samples.pop_front();
        }
        if (!samples.empty())
                samples.clear();
    }
    if (harmonizedIsTrue)
        for (int i = 0; i < 44; ++i)
            for (int j = 88200 / 64; j < 88200; ++j)
                strings[i][j] += strings[i + 7][j];
}

int main(int argc, char *argv[])
{
    QApplication app(argc, argv);
    OurWindow * window = new OurWindow;
    window->show();

    srand(time(NULL));
    BASS_INFO info;

    BASS_SetConfig(BASS_CONFIG_UPDATEPERIOD, 1);
    BASS_Init(-1, 44100, BASS_DEVICE_LATENCY, 0, NULL);
    BASS_GetInfo(&info);
    BASS_SetConfig(BASS_CONFIG_BUFFER, 200);

    run_all();

    for (int i = 0; i <= 5; ++i)
    {
            streams[i] = BASS_StreamCreate(44100, 1, 0, STREAMPROC_PUSH, 0);
            BASS_ChannelPlay(streams[i], FALSE);
    }

    return app.exec();
}
